<?php
/**
 * 服务器cli命令行工具
 *
 * @author		黑冰 <954650386@qq.com>
 */

namespace waf\cmd;

use \waf\Waf;
use \waf\log\WLog;

class WCmd
{
    public static function deal()
    {
        WLog::logSystem("in", __FILE__, __LINE__, __FUNCTION__);
        $cmd = trim($_SERVER['argv'][1]);
        if($cmd)
        {
            if($cmd == 'stop')
            {
                $file =  Waf::getConfig('log', 'log_path').'/master_'.Waf::getConfig('server', 'name').'.pid';
                if($file && file_exists($file))
                {
                    $file_content = file_get_contents($file);
                    if($file_content)
                    {
                        $file_content = explode('|', $file_content);
                        if($file_content && is_array($file_content) && intval($file_content[1]))
                        {
                            shell_exec("kill -15 ".$file_content[1]);
                        }
                    }
                }
                exit;
            }
            else if($cmd == 'stopAll')
            {
                $arrRet = self::getServerProcessInfo(Waf::getConfig('server', 'name'));
                if($arrRet)
                {
                    foreach($arrRet as $v)
                    {
                        $a = shell_exec("kill -15 ".$v['pid']);
                    }
                }
                exit;
            }
            else if($cmd == 'start')
            {

            }
            else if($cmd == 'test')
            {
                echo "begin test ... \t\n";
                print_r(self::getServerProcessInfo('testtcp'));
                exit;
            }
            else{
                echo "ep: php ./index.php start | stop | stopAll \t\n";
                exit;
            }
        }else{
            echo "ep: php ./index.php start | stop | stopAll \t\n";
            exit;
        }

        WLog::logSystem("out", __FILE__, __LINE__, __FUNCTION__);
    }

    public static function getServerProcessInfo($server_name = '')
    {
        $arrRet = array();
        WLog::logSystem("in. server_name=$server_name", __FILE__, __LINE__, __FUNCTION__);

        if($server_name)
        {
            $shell = "ps -e -o 'pid,args,pcpu,pmem,rsz,lstart,s' |grep {$server_name}_ |grep -v grep |awk '{print $1\"|\"$2\"|\"$3\"|\"$4\"|\"$5\"|\"$7\"|\"$8\"|\"$9\"|\"$10\"|\"$11}'";
            WLog::logSystem("shell = $shell", __FILE__, __LINE__, __FUNCTION__);
            $ret = shell_exec($shell);

            WLog::logSystem("ret = ".WLog::toJson($ret), __FILE__, __LINE__, __FUNCTION__);

            if($ret)
            {
                WLog::logSystem("find.", __FILE__, __LINE__, __FUNCTION__);
                $arrT = explode("\n", $ret);

                if($arrT && is_array($arrT))
                {
                    foreach($arrT as $v)
                    {
                        if($v)
                        {
                            $arrT2 = explode("|", $v);
                            WLog::logSystem("v : ".WLog::toJson($arrT2), __FILE__, __LINE__, __FUNCTION__);
                            if($arrT2 && is_array($arrT2))
                            {
                                $arrT3 = array();
                                $arrT3['pid']           = $arrT2[0];
                                $arrT3['work_name']     = $arrT2[1];
                                $arrT3['pcpu']          = $arrT2[2];
                                $arrT3['pmem']          = $arrT2[3];
                                $arrT3['memory']        = $arrT2[4];
                                $arrT3['start_time']    = $arrT2[8].'-'.self::getNumber2(self::getMonth($arrT2[5])).'-'.self::getNumber2($arrT2[6]).' '.$arrT2[7];
                                $arrT3['status']        = $arrT2[9];

                                $arrRet[] = $arrT3;
                            }
                        }
                    }
                }
            }else{
                WLog::logSystem("not find.", __FILE__, __LINE__, __FUNCTION__);
            }
        }

        WLog::logSystem("out. arrRet".WLog::toJson($arrRet), __FILE__, __LINE__, __FUNCTION__);
        return $arrRet;
    }

    public static function getMonth($month = '', $type = 2)
    {
        WLog::logSystem("in. month=$month, type=$type", __FILE__, __LINE__, __FUNCTION__);
        $ret = 0;

        $Month_E = array(
            1 => "Jan",
            2 => "Feb",
            3 => "Mar",
            4 => "Apr",
            5 => "May",
            6 => "Jun",
            7 => "Jul",
            8 => "Aug",
            9 => "Sep",
            10 => "Oct",
            11 => "Nov",
            12 => "Dec");

        $Month_num = array(
            "Jan"   => 1,
            "Feb"   => 2,
            "Mar"   => 3,
            "Apr"   => 4,
            "May"   => 5,
            "Jun"   => 6,
            "Jul"   => 7,
            "Aug"   => 8,
            "Sep"   => 9,
            "Oct"   => 10,
            "Nov"   => 11,
            "Dec"   => 12);

        if($type == 1)
        {
            $ret = $Month_E[$month];
        }else if($type == 2)
        {
            $ret = $Month_num[$month];
        }

        WLog::logSystem("out. ret = $ret", __FILE__, __LINE__, __FUNCTION__);
        return $ret;
    }

    public static function getNumber2($number = 0)
    {
        $ret = '';

        if($number >= 10)
        {
            $ret = $number;
        }else{
            $ret = '0'.$number;
        }

        return $ret;
    }
}
?>
